const Goods = require('../../model/goods');
module.exports = async (req,res) => {
    const { sort, buyOrSale, level, count, price, transaction} = req.body;
    const resData = {
        // data返回的是用户传递过来的数据
        data: [],
        meta: {
            code: 200,
            msg: "查找成功!",
        },
    };
    var goodsAll = [];
    var goods = [];
    if(sort.length != 0 || level.length !=0 || count.length != 0 || transaction.length !=0 || price.length != 0 || buyOrSale.length != 0) {
        for(var i = 0; i < buyOrSale.length; i++) {
            // console.log(/${buyOrSale[0]}/);
            goods = await Goods.find({ sort: buyOrSale[0] == '求购' ? /^求购-/ : /^求售-/ })
            goodsAll.push(...goods)
        }
        if(buyOrSale.length == 0) {
            goods = await Goods.find()
            goodsAll.push(...goods)
        }
        // for(var item in sort) {
        //     sort[item] = buyOrSale[0] + '-' + sort[item];
        // }
        for(var i = 0; i < level.length; i++) {
            // var tempGoods = await Goods.find({ level: level[i]})
            goods = goodsAll.filter(item => item.level == level[i])

            // goods.push(...tempGoods)
        }
        for(var i = 0; i < sort.length; i++) {
            // var reg = new RegExp(`${sort[i]}$`)
            // var sortFunc = (sortName) => {
            //     switch(sortName) {
            //         case '电子产品': return /电子产品$/;
            //         case '图书': return /图书$/;
            //         case '服装': return /服装$/;
            //         case '日用品': return /日用品$/;
            //         case '玩具': return /玩具$/;
            //         case '奢侈品': return /奢侈品$/;
            //         case '食品': return /食品$/;
            //         case '乐器': return /乐器$/;
            //     }
            // }
            // console.log(sortFunc(sort[i]));
            if(buyOrSale.length != 0) {
                // console.log(goodsAll[0].sort, '上');
                if(i > 0) {
                    goods.push(...goodsAll.filter(item => item.sort == buyOrSale[0] + '-' + sort[i]))
                } else {
                    goods = goodsAll.filter(item => item.sort == buyOrSale[0] + '-' + sort[i])
                }
            }else {
                // console.log(sort[i]);
                // console.log(goodsAll); // 这里输出是空
                
                // goods.push(...goods.filter(item => item.sort == sortFunc(sort[i])))
                if(i > 0) {
                    // sort[i]
                    // console.log(sortFunc(sort[i]).test(goods[i].sort), 'haha'); // 这样用test是没错的
                    // console.log(goodsAll.filter(item => sortFunc(sort[i]).test(item.sort)));
                    // goods.push(...goodsAll.filter(item => sortFunc(sort[i]).test(item.sort)))
                    // goods.push(...goodsAll.filter(item => item.sort.test(sortFunc(sort[i]))))
                    goods.push(...goodsAll.filter(item => item.sort.substring(3) == sort[i]))
                } else {
                    // console.log(goodsAll.filter(item => sortFunc(sort[i]).test(item.sort)));
                    // goods.push(...goodsAll.filter(item => sortFunc(sort[i]).test(item.sort)))
                    goods = goodsAll.filter(item => item.sort.substring(3) == sort[i])
                    // goods = goodsAll.filter(item => sortFunc(sort[i]).test(item.sort))
                }
                
            }
            
            // console.log(reg);
            
            // var tempGoods = await Goods.find({ sort: sortFunc(sort[i]) })
            // goods.push(...tempGoods);
        }
        for(var i = 0; i < price.length; i++) {
            // var tempGoods = await Goods.find({ level: level[i]})
            // console.log(price[i].substring(2))
            goods = goodsAll.filter(item => item.price >= price[i].substring(2))
            // goods.push(...tempGoods)
        }
        for(var i = 0; i < count.length; i++) {
            // var tempGoods = await Goods.find({ level: level[i]})
            // console.log(count[i].substring(2))
            goods = goodsAll.filter(item => item.count >= count[i].substring(2))
            // goods.push(...tempGoods)
        }
        for(var i = 0; i < transaction.length; i++) {
            // var tempGoods = await Goods.find({ level: level[i]})
            // console.log(count[i].substring(2))
            goods = goodsAll.filter(item => item.transaction == transaction[i])
            // goods.push(...tempGoods)
        }
        
    } else {
        goods = await Goods.find()
    }
    resData.data = goods;
    res.send(resData);
}